<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Mmod_conversion extends MYAD_Model{
	protected $table_name = 'm_currencyconversionrate';
	protected $table_company = 'm_company';
        function __construct(){
			parent::__construct();
		}
        /**
        * Function get new currency rate 
        * 
        */
       function getOnlyNewCurrency() {       
            $this->db->limit(1);
            $this->db->order_by("ApplyDate", "desc");
            $this->db->select('*');
            $this->db->from('m_currencyconversionrate');
            $query = $this->db->get();            
            return $query->result_array();
            
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    Get all data for view of Conversion Rate list
         * @Input :         Null
         * @Output :        (Array) data for view data
         */       
        function getAllConversionRate($page=0){
            /* load pagination */
            $pagiData = array(
                'base_url' => base_url('conversion/'),
                'total_rows' => $this->count_all(),
            );
            $pagination = $this->config->item('pagination');
            $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
            $pagi = $this->getPagination($pagiData);
            $data = array(
                'show' => 'index',
                'pagi' => $pagi,
                'title' => $this->lang->line('CR_title'),
                'order' => $page,
            );            
            /* load conversion */
            $conversionData = $this->getConversionData($page, $offset);
            return array_merge($data,$conversionData);
        }
        /*
         * @author :        HungLV
         * @Description:    Get Conversion rate data
         * @Input :         
         *                  (Integer) $limit
         *                  (Integer) $offset
         * @Output :        Array data
         */       
        function getConversionData($limit = Null, $offset = Null){
            $this->db->from($this->table_name.' as a ');
            $this->db->join($this->table_company.' as com ', 'com.CompanyCode = a.CompanyCode');
            $this->db->where('a.CompanyCode',$this->session->userdata('CompanyCode'));
            if($this->session->userdata('Level')==2){
                $this->db->where('Del_Flag',0);
            }
            if ($limit !== Null && $offset !== Null) {
                $this->db->limit($offset, $limit);
            } elseif ($limit !== NULL) {
                $this->db->limit($limit);
            }
            $query = $this->db->get();
            $result = $query->result_array();
            return array('data' => $result);
        }
        /*
         * @author :        HungLV
         * @Description:    Validate All
         * @Input :         
         * @Output :        Return true or false
         */        
        function validate(){
            $_POST['txtRate'] = mb_convert_kana($_POST['txtRate'], 'a');
            $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'a');        
            $this->form_validation->set_error_delimiters('', '');
            $this->form_validation->set_rules('txtRate',$this->lang->line('CR_Rate'),'required|integer|max_length[5]|greater_than[0]'); 
            $this->form_validation->set_rules('txtApplyDate',$this->lang->line('CR_Applydate'), 'required|callback_validate_date');
            return $this->form_validation->run();
        }
        /*
         * @author :        HungLV
         * @Description:    Insert data to database
         * @Input :         
         * @Output :        Return true or false
         */        
        function insertDataConversion(){
            // get txtRate, txtApplyDate
            $data = array(
                'CompanyCode' => $this->session->userdata('CompanyCode'),
                'Rate' => $this->input->post('txtRate'),
                'ApplyDate' => date('Y-m-d',strtotime( $this->input->post('txtApplyDate'))),
            );
            $result = $this->updateDB($this->table_name,$data);
            return $result;
        }
        /*
         * @author :        HungLV
         * @Description:    update data to database
         * @Input :         
         * @Output :        Return true or false
         */        
        function updateDataConversion($id){
            // get txtRate, txtApplyDate
            $data = array(
                'Rate' => $this->input->post('txtRate'),
                'ApplyDate' => date('Y-m-d',strtotime( $this->input->post('txtApplyDate'))),
                'Del_Flag' => $this->input->post('chkDelFlag'),
            );
            $result = $this->updateDB($this->table_name,$data,array('RateId'=>$id));
            return $result;
        }        
        /*
         * @author :        HungLV
         * @Description:    get info for view
         * @Input :         (String)$name
         * @Output :        Return array
         */        
        function getInfoData($title,$name){
            $data['js'] = array('jquery-ui.js');
            $data['css'] = array('jquery-ui.css');		
            $data['title'] = $title;
            $data['show'] = $name;
            return $data;
        }
        /*
         * @author :        HungLV
         * @Description:    Check Exist conversion by Id
         * @Input :         (Number)$id
         * @Output :        Return True or False
         */        
        function checkExist($id){
            $this->db->where('RateId',$id);
            $this->db->where('CompanyCode',$this->session->userdata('CompanyCode'));
            $query = $this->db->get($this->table_name);
            if($query->num_rows()>0){
                return TRUE;
            }
            else{
                return FALSE;
            }
        }
        /*
         * @author :        HungLV
         * @Description:    Check Duplicate Conversion
         * @Input :         
         * @Output :        Return True or False
         */         
        function checkDuplicateConversion($date,$id=0){
            $this->db->from($this->table_name.' as a ');
            $this->db->join($this->table_company.' as com ', 'com.CompanyCode = a.CompanyCode');
            $this->db->where('ApplyDate',$date);
            $this->db->where('Del_Flag',0);
            $query = $this->db->get();
            if($id!=0){
                if($query->num_rows() >0){
                    $result = $query->row_array();
                    if($id == $result['RateId']){
                        return TRUE;
                    }
                    else{
                        return FALSE;
                    }
                }
                else{
                    return TRUE;
                }
            }
            else{
                if($query->num_rows() >0){
                    return FALSE;
                }
                else{
                    return TRUE;
                }
            }
        }
}
       
?>